package com.ptit.giaothong;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ptit.giaothong.bienbao.BienbaoActivity;
import com.ptit.giaothong.help.HelpActivity;
import com.ptit.giaothong.taxi.TaxiActivity;
import com.ptit.giaothong.tracnghiem.PlayGame;
import com.ptit.giaothong.xebus.XebusActivity;

public class MainActivity extends Activity {

	private LinearLayout topten, line_setupline, line_game, line_taxi,
			line_help, line_luat, line_bus;
	public static int keypage = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		init();
	}

	public void init() {
		keypage = 0;
		setContentView(R.layout.menu_home);
		line_game = (LinearLayout) findViewById(R.id.line_game);
		line_taxi = (LinearLayout) findViewById(R.id.line_taxi);
		line_help = (LinearLayout) findViewById(R.id.line_help);
		line_luat = (LinearLayout) findViewById(R.id.line_luat);
		line_bus = (LinearLayout) findViewById(R.id.line_phone);
		line_setupline = (LinearLayout) findViewById(R.id.setupline);
		topten = (LinearLayout) findViewById(R.id.topten);

		line_game.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				PlayGame playGame = new PlayGame(MainActivity.this);
				playGame.init(1);
			}
		});

		line_taxi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				TaxiActivity taxiActivity = new TaxiActivity(MainActivity.this);
				taxiActivity.init(1);
			}
		});

		line_help.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				HelpActivity helpActivity = new HelpActivity(MainActivity.this);
				helpActivity.init();
			}
		});
		line_luat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				BienbaoActivity bActivity = new BienbaoActivity(
						MainActivity.this);
				bActivity.init(1);
			}
		});

		line_bus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				XebusActivity xebusActivity = new XebusActivity(
						MainActivity.this);
				xebusActivity.init();
			}
		});
		line_setupline.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Setup setup = new Setup(MainActivity.this);
				setup.init();
			}
		});
		topten.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				HeightScores heightScores=new HeightScores(MainActivity.this);
				heightScores.init();
			}
		});

	}

	public static void getdialogMesg(final Activity activity, String msg) {
		final Dialog dialog = new Dialog(activity);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(R.drawable.bgr_dialog);

		dialog.setContentView(R.layout.dialog);

		// set the custom dialog components - text, image and button
		TextView text = (TextView) dialog.findViewById(R.id.txtmesg);
		text.setText(msg);

		Button dialogButton = (Button) dialog.findViewById(R.id.btn_no);
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		Button thoat = (Button) dialog.findViewById(R.id.btn_yes);
		// if button is clicked, close the custom dialog
		thoat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				activity.finish();
			}
		});
		dialog.show();

	}

	@Override
	protected void onPause() {
		 if (PlayGame.countDownTimer != null) {
		 PlayGame.countDownTimer.cancel();
		 }
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (PlayGame.countDownTimer != null) {
			PlayGame.countDownTimer.start();
		}
		super.onResume();
	}

	@Override
	public void onBackPressed() {
		// super.onBackPressed();
		switch (keypage) {
		case 0:
			finish();
			System.exit(0);
			break;
		case 1:
			if (PlayGame.countDownTimer != null) {
				PlayGame.countDownTimer.cancel();
			}
			init();
			break;
		}

	}
}